package com.bothin.experiment4.controller;

import com.bothin.experiment4.constant.NoticeNumConstant;
import com.bothin.experiment4.dto.AnalyDTO;
import com.bothin.experiment4.dto.StatisticDTO;
import com.bothin.experiment4.dto.SumaryDTO;
import com.bothin.experiment4.service.IStatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by bothin
 * At 2019/12/20 20:48
 * Description:
 */
@Controller
@RequestMapping("/statistics")
@Secured("ROLE_USER_ADMIN")
public class StatisticsController {

    @Autowired
    private IStatisticService statisticService;

    @RequestMapping("/company/to/sum")
    public ModelAndView get_company_to_sum(){

        return new ModelAndView("/statistics/company_sum");
    }

    @RequestMapping("/company/sum")
    @ResponseBody
    public List<SumaryDTO> get_company_sum(@RequestParam(value = "start",defaultValue = "") String start,
                                           @RequestParam(value = "end",defaultValue = "") String end){
        if (StringUtils.isEmpty(start)||StringUtils.isEmpty(end)){
            return statisticService.getTheSumEachMouthOfCompany(NoticeNumConstant.TEST_YEAR).stream()
                    .peek(e->e.setLabel(e.getLabel()+"月")).collect(Collectors.toList());
        }else {
            List<SumaryDTO> temp = new ArrayList<>();
            temp.add(statisticService.getTheSumAPeriodOfTime(start,end));
            return temp;
        }
    }

    @RequestMapping("/customer/to/analy")
    public ModelAndView customer_to_analy(){
        return new ModelAndView("/statistics/customer_analy");
    }

    @RequestMapping("/customer/analy")
    @ResponseBody
    public AnalyDTO customer_analy(@RequestParam(value = "start",defaultValue = "") String start,
                                   @RequestParam(value = "end",defaultValue = "") String end){
        if (StringUtils.isEmpty(start)||StringUtils.isEmpty(end)){
            return new AnalyDTO(statisticService.getAllCustomerStatistic());
        }else {
            return new AnalyDTO(statisticService.getAllCustomerStatistic(start,end,20));
        }
    }

    @RequestMapping("/product/to/analy")
    public ModelAndView product_to_analy(){
        return new ModelAndView("/statistics/product_analy");
    }

    @RequestMapping("/product/analy")
    @ResponseBody
    public AnalyDTO product_analy(@RequestParam(value = "start",defaultValue = "") String start,
                                  @RequestParam(value = "end",defaultValue = "") String end){
        if (StringUtils.isEmpty(start)||StringUtils.isEmpty(end)){
            return new AnalyDTO(statisticService.getAllProductSStatistic());
        }else {
            return new AnalyDTO(statisticService.getAllProductSStatistic(start,end,20));
        }
    }


}
